<template>
	<view class="navbar" style="background: linear-gradient(#EE3F4D 35%,#F0A1A8,#FFFFFF);">
		<view class="navbar-fixed">
			<!-- 状态栏小程序撑起高度 -->
			<view :style="{height:statusBarHeight+'px'}"></view>
			<view class="navbar-content" :style="{height:navBarHeight+'px',width:windowWidth+'px'}">
				<uni-icons v-if="isback" type="left" size="30" color="#fff" @click="back"></uni-icons>
				<view class="title" :style="{'margin-left':width+'px'}">
					<text class="text-bold text-xxl text-white">{{text}}</text>
				</view>
			</view>
			<slot>
			</slot>
		</view>
		<!-- 需要添加占位符高度  状态栏高度+导航栏高度（否则下面tab会塌陷）-->
		<!-- <view :style="{height: statusBarHeight+navBarHeight+'px'}"></view> -->
	</view>
</template>

<script>
	export default {
		name: 'navbar',
		props: {
			text: {
				type: String,
				default () {
					return '资讯'
				}
			},
			isback: {
				type: Boolean,
				default () {
					return false
				}
			}
		},
		data() {
			return {
				width:200,
				statusBarHeight: 20,
				/* 状态栏高度 */
				navBarHeight: 45,
				/* 导航栏高度 */
				windowWidth: 375,
				/* 窗口宽度 */
				/* 设定状态栏默认高度 */
				val: '' /* 导航栏搜索框的值 */
			};
		},
		created() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
			this.statusBarHeight = info.statusBarHeight
			this.windowWidth = info.windowWidth
			// 除了h5 app mp-alipay的情况下执行
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo);
			this.width = menuButtonInfo.width
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info
				.statusBarHeight)
			this.windowWidth = menuButtonInfo.left
			// #endif
		},
		methods: {
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-fixed {
			// width: 100%;

			.navbar-content {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100px;
				box-sizing: border-box;
				padding-left: 20rpx;

				.title {
					display: flex;
					align-items: center;
					width: 100%;
					height: 45px;
					justify-content: center;
				}
			}

		}

	}
</style>